package delphorm.utils;

import java.io.FileNotFoundException;
import java.util.Properties;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

/**
 * Classe utilitaire pour manipuler la base de données
 * 
 * @author jerome FIXME a terminer
 */
public class DatabaseUtils {
	/**
	 * Permet d'exporter le schéma en format SQL à partir des fichirs de mapping
	 * Hibernate
	 * 
	 * @param filename
	 *            Nom du fichier où réaliser l'export
	 * @throws FileNotFoundException
	 */
	public static void exportSchema(String filename)
			throws FileNotFoundException {
		
		new SchemaExport(createConfig()).setOutputFile(filename).create(true, false);

	}

	public static void createSchema()
	{
		new SchemaExport(createConfig()).create(false,true);
	}
	
	private static Configuration createConfig()
	{
		return new Configuration()
		.addResource("delphorm/dao/hibernate/utilisateur.hbm.xml")
		.addResource("delphorm/dao/hibernate/groupe.hbm.xml")
		.addResource("delphorm/dao/hibernate/groupecollectif.hbm.xml")
		.addResource("delphorm/dao/hibernate/groupeutilisateur.hbm.xml")
		.addResource("delphorm/dao/hibernate/droitGroupe.hbm.xml")
		.addResource("delphorm/dao/hibernate/droitQuestionnaire.hbm.xml")
		.addResource("delphorm/dao/hibernate/question.hbm.xml")
		.addResource("delphorm/dao/hibernate/questionnaire.hbm.xml")
		.addResource("delphorm/dao/hibernate/reponse.hbm.xml")
		.addResource("delphorm/dao/hibernate/reponseOuverte.hbm.xml")
		.addResource("delphorm/dao/hibernate/reponseFermee.hbm.xml")
		.addResource("delphorm/dao/hibernate/valeur.hbm.xml")
		.addResource("delphorm/dao/hibernate/valeurTexte.hbm.xml")
		.addResource("delphorm/dao/hibernate/valeurNumerique.hbm.xml")
		.addResource("delphorm/dao/hibernate/valeurFichier.hbm.xml")
		.addResource("delphorm/dao/hibernate/instanceQuestionnaire.hbm.xml")
		.addResource("delphorm/dao/hibernate/instanceQuestion.hbm.xml")
		.addResource("delphorm/dao/hibernate/instanceReponse.hbm.xml")
		.addResource("delphorm/dao/hibernate/instanceReponseFermee.hbm.xml")
		.addResource("delphorm/dao/hibernate/instanceReponseOuverte.hbm.xml")
		.addResource("delphorm/dao/hibernate/filtre.hbm.xml")
		.addResource("delphorm/dao/hibernate/filtreNumerique.hbm.xml")
		.addResource("delphorm/dao/hibernate/filtreTexte.hbm.xml")
		.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect")
		.setProperty("current_session_context_class", "thread")
		.setProperty("current_session_context_class", "thread");
	}
	public static LocalSessionFactoryBean buildSessionFactory() {
		DriverManagerDataSource datasource;
		datasource = new DriverManagerDataSource();
		datasource.setDriverClassName("org.postgresql.Driver");
		datasource.setUsername("admin");
		datasource.setPassword("admin");
		datasource.setUrl("jdbc:postgresql://localhost:5432/autoformdb");

		LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
		sessionFactory.setDataSource(datasource);
		String[] fichiersmapping = new String[] {
				"mapping/utilisateur.hbm.xml", "mapping/groupe.hbm.xml",
				"mapping/droitgroupe.hbm.xml", "mapping/question.hbm.xml",
				"mapping/questionnaire.hbm.xml", "mapping/reponse.hbm.xml",
				"mapping/reponseOuverte.hbm.xml",
				"mapping/reponseFermee.hbm.xml", "mapping/valeur.hbm.xml",
				"mapping/valeurTexte.hbm.xml",
				"mapping/valeurNumerique.hbm.xml",
				"mapping/valeurFichier.hbm.xml",
				"mapping/instanceQuestionnaire.hbm.xml",
				"mapping/instanceQuestion.hbm.xml",
				"mapping/instanceReponse.hbm.xml",
				"mapping/instanceReponseOuverte.hbm.xml",
				"mapping/instanceReponseFermee.hbm.xml",
				"mapping/filtre.hbm.xml", "mapping/filtreNumerique.hbm.xml",
				"mapping/filtreTexte.hbm.xml" };
		sessionFactory.setMappingResources(fichiersmapping);
		Properties prop = new Properties();
		prop
				.put("hibernate.dialect",
						"org.hibernate.dialect.PostgreSQLDialect");
		prop.put("current_session_context_class", "thread");
		prop.put("show_sql", "true");
		sessionFactory.setHibernateProperties(prop);

		return sessionFactory;
	}
}
